local skel = fk.CreateSkill {
  name = "rmt__lianxuju",
}

Fk:loadTranslationTable{
  ["rmt__lianxuju"] = "连续剧",
  [":rmt__lianxuju"] = "回合结束时，你可以令X名角色的名字替换为易？剧，并获得〖大剧道〗（X为你本回合使用过牌的阶段数）。",

  ["#rmt__lianxuju-choose"] = "连续剧：你可以令至多%arg名角色变为易X剧！",
  ["$rmt__lianxuju1"] = "哇！",
}

skel:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local room = player.room
      local dat = {}
      room.logic:getEventsOfScope(GameEvent.Phase, 1, function (e)
        if e.data.who == player then
          table.insert(dat, {e.id, e.end_id})
        end
      end, Player.HistoryTurn)
      if #dat == 0 then return end
      local n = 0
      for _, info in ipairs(dat) do
        if #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
          return e.id >= info[1] and e.id <= info[2] and e.data.from == player
        end, Player.HistoryTurn) > 0 then
          n = n + 1
        end
      end
      if n > 0 then
        event:setCostData(self, {n = n})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local n = event:getCostData(self).n
    local tos = room:askToChoosePlayers(player, {
      targets = room.alive_players, min_num = 1, max_num = n,
      prompt = "#rmt__lianxuju-choose:::"..n, skill_name = skel.name,
    })
    if #tos > 0 then
      room:sortByAction(tos)
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    if not tos then return end
    local generals = {}
    for i = 1, 10 do
      table.insert(generals, "rmt"..i.."_yidaju")
    end
    for _, p in ipairs(room.players) do
      if not table.contains(tos, p) then
        table.removeOne(generals, p.general)
      end
    end
    table.shuffle(generals)
    for _, to in ipairs(tos) do
      local name = "rmt"..math.random(1, 10).."_yidaju"
      if #generals > 0 then -- 优先选择未重复的
        name = table.remove(generals, 1)
      end
      room:setPlayerProperty(to, "general", name)
      room:handleAddLoseSkills(to, "rmt__dajudao")
    end
  end,
})

return skel
